package fw.helpers;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import fw.ApplicationManager;

public class AjaxHelper {
	protected ApplicationManager app;
	JavascriptExecutor js;

	public AjaxHelper(ApplicationManager app) {
		this.app = app;
		js = (JavascriptExecutor) app.getDriver();
	}

	/**
	 * Waits for Ajax to complete
	 */
	public void waitForAjax() {
		int i = 0;
		while (i < 10) {
			boolean ajaxIsComplete = (Boolean) js.executeScript("return jQuery.active == 0");
			if (ajaxIsComplete) {
				break;
			}
			app.webElement().pause(1);
			i++;
		}
	}

	/**
	 * Scrolls down to WebElement if needed
	 * 
	 * @param p_element
	 */
	public void scrollDown(WebElement p_element) {
		try {
			js.executeScript("arguments[0].scrollIntoView(true);", p_element);
		} catch (Exception ex) {
		}
	}
}
